from rest_framework import serializers

from .models import Job, JobReport


class JobSerializer(serializers.Serializer):

	id = serializers.IntegerField(read_only=True)
	url = serializers.URLField(required=True, label='接口地址')
	params = serializers.CharField(label='接口参数')
	case_id = serializers.IntegerField(required=True, label='用例ID')
	trigger = serializers.CharField(required=True, label='触发器')
	job_cron = serializers.CharField(required=True, label='corn表达式')
	task_id = serializers.CharField(required=True, label='调度ID')
	job_status = serializers.BooleanField(read_only=True, label='任务状态')

	class Meta:
		model = Job

	def create(self, validated_data):
		job = Job.objects.create(**validated_data)
		return job


class JobReportSerializer(serializers.Serializer):
	id = serializers.IntegerField(read_only=True)
	case_id = serializers.CharField(required=True, label='接口ID')
	task_id = serializers.CharField(required=True, label='调度ID')
	expect = serializers.CharField(required=True, label='预期')
	actual = serializers.BooleanField(required=True, label='实际结果')
	response_data = serializers.CharField(required=True, label='返回结果')
	report_time = serializers.DateTimeField(read_only=True)

	class Meta:
		model = JobReport

	def create(self, validated_data):
		report = JobReport.objects.create(**validated_data)
		return report
